import pathlib
from typing import List
from functools import lru_cache
from pydantic_settings import BaseSettings, SettingsConfigDict



class Settings(BaseSettings):

    # 基本配置
    DESC: str = "Agent智能体API"
    VERSION: str = "0.8.1"
    LOG_LEVEL: str = "DEBUG"
    WEB_HOST: str = "0.0.0.0"
    WEB_PORT: int = 5666  # 默认端口
    BASE_URL: str = "http://172.19.177.2"


    # 是否初始化 checkpointer
    INIT_START: bool = True

    # 项目路径
    PROJECT_PATH: pathlib.Path = pathlib.Path(__file__).parent.parent
    LOG_PATH: pathlib.Path = PROJECT_PATH / "logs"  # 日志路径

    # 静态文件
    STATIC_FILE: bool = True
    STATIC_PATH: pathlib.Path = PROJECT_PATH / "static"

    # PostgreSQL配置
    POSTGRES_HOST: str = "172.16.93.205"
    POSTGRES_PORT: int = 5432
    POSTGRES_USER: str = "postgres"
    POSTGRES_PASSWORD: str = "123456"
    POSTGRES_DB: str = "postgres"


    @property
    def DB_URI(self) -> str:
        print(f"postgresql://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}@{self.POSTGRES_HOST}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}?sslmode=disable")
        return f"postgresql://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}@{self.POSTGRES_HOST}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}?sslmode=disable"

    
    # Redis配置
    REDIS_HOST: str = "172.16.93.205"
    REDIS_PORT: int = 6379
    REDIS_PASSWORD: str = "123456"
    REDIS_DB: int = 4
    REDIS_MAX_CONNECTIONS: int = 20
    REDIS_TIMEOUT: int = 30



    @property
    def REDIS_URI(self) -> str:
        print(f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}?password={self.REDIS_PASSWORD}")
        return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}?password={self.REDIS_PASSWORD}"
    
    # CORS配置
    CORS_ORIGINS: List[str] = ["*"]  # 允许的跨域请求来源，默认允许所有来源


    # Qwen32 模型配置
    QWEN32_MODEL: str = "./qwen3-32b"
    QWEN32_BASE_URL: str = "http://172.16.53.17:51090/v1"
    QWEN32_API_KEY: str = "sk-xxx"


    # Qwen235 模型配置
    QWEN235_MODEL: str = "qwen3"
    QWEN235_BASE_URL: str = "http://172.16.53.70:31963/v1"
    QWEN235_API_KEY: str = "sk-xxx"

    # DeepSeek 模型配置
    DEEPSEEK_MODEL: str = ""
    DEEPSEEK_BASE_URL: str = ""
    DEEPSEEK_API_KEY: str = ""

    
    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding='utf-8',
        case_sensitive=True,
        extra='ignore'
        )
    
    def __init__(self, **values):
        super().__init__(**values)
        self.LOG_PATH.mkdir(parents=True, exist_ok=True)

@lru_cache
def get_settings():
    """ 缓存配置 """
    return Settings()


settings = get_settings()

print("Loaded settings:", settings.model_dump())

# 检查 env 是否成功读取
print("POSTGRES_HOST:", settings.POSTGRES_HOST)